package hu.bme.aut.consulttime.client.components;

import java.util.List;

import hu.bme.aut.consulttime.client.StudentService;
import hu.bme.aut.consulttime.client.StudentServiceAsync;
import hu.bme.aut.consulttime.entitydtos.UserDTO;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.FlexTable;

public class StudentPage extends Composite {
	// Proxy to studentService
	private static StudentServiceAsync studentService = GWT
			.create(StudentService.class);

	private static Button btnCreate;
	private static VerticalPanel mainPanel;
	private static FlexTable flexTable;

	public StudentPage() {

		mainPanel = new VerticalPanel();
		initWidget(mainPanel);

		btnCreate = new Button("Add New Student");
		btnCreate.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new CreateStudent().center();
			}
		});
		mainPanel.add(btnCreate);
		mainPanel.setCellHorizontalAlignment(btnCreate,
				HasHorizontalAlignment.ALIGN_RIGHT);

		//loadStudents();
	}

	public static void loadStudents() {
		if (flexTable != null) {
			mainPanel.remove(flexTable);
		}
		flexTable = new FlexTable();
//		flexTable.setWidth("100%");
		flexTable.setBorderWidth(1);

		// Add these lines
		flexTable.setText(0, 0, "User Name");
		flexTable.setText(0, 1, "Name");
		flexTable.setText(0, 2, "Email");
		flexTable.setText(0, 3, "Change Password");
		flexTable.setText(0, 4, "Remove");
		
		studentService.retrieveStudents(new AsyncCallback<List<UserDTO>>() {
			@Override
			public void onSuccess(List<UserDTO> result) {
				for(int i = 0; i < result.size(); ++i){
					final UserDTO tmp = result.get(i);
					
					flexTable.setText(i+1, 0, tmp.getUserName());
					flexTable.setText(i+1, 1, tmp.getFirstName() + " " + tmp.getLastName());
					flexTable.setText(i+1, 2, tmp.getEmail());
					
					// add button to modify this user from the list
				    Button modifyUser = new Button("Modify");
				    modifyUser.addClickHandler(new ClickHandler() {
				    public void onClick(ClickEvent event) {	
				    	new ModifyStudentPassword(tmp.getUserId(), tmp.getUserName()).center();
				    }
				    });
				    
					flexTable.setWidget(i+1, 3, modifyUser);
					
					 // add button to remove this user from the list
				    Button removeUser = new Button("Delete");
				    removeUser.addClickHandler(new ClickHandler() {
				    public void onClick(ClickEvent event) {	
				    	//Remove the User - inner AsyncCall
				        studentService.deleteStudent(tmp.getUserId(), new AsyncCallback<Boolean>() {
							
							@Override
							public void onSuccess(Boolean result) {
								if(result == true){
									Window.confirm("'"+tmp.getUserName() + "' user deleted!");
								}else{
									Window.alert("Student deletion failed!");
								}
								
						        //Refresh the table
						        loadStudents();
							}
							
							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Remote Procedure Call - Failure");
							}
						});	       
				    }
				    });
				    
				    flexTable.setWidget(i+1, 4, removeUser);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Remote Procedure Call - Failure");
			}
		});
		mainPanel.add(flexTable);
	}
	
}
